﻿(function ($) {
    $(window).bind("resize", function () {
        var newWidth = $(window).width();
        var newHeight = $(window).height();
        if (EIAP.wWidth != newWidth || EIAP.wHeight != newHeight) {
            var wRatio = newWidth / EIAP.wWidth;
            var hRatio = newHeight / EIAP.wHeight;
            EIAP.wWidth = newWidth;
            EIAP.wHeight = newHeight;
            EIAP.resizeAll(wRatio, hRatio);
        }
    });
    window.EIAP = $.eiapui = {
        version: 'v2.6.4.001',
        namespace: function() {
            var o, d, s;
            for (var i = 0; i < arguments.length; i++) {
                d = arguments[i].split(".");
                o = window[d[0]] = window[d[0]] || {};
                s = d.slice(1);
                for (var m = 0; m < s.length; m++) {
                    o = o[s[m]] = o[s[m]] || {};
                }
            }
            return o;
        },
        //isIE: $.browser.msie ? true : false,
        wHeight: $(window).height(),
        wWidth: $(window).width(),
        //BLANK_IMAGE_URL: 'http://' + window.location.host + '/EIAP/resources/images/s.gif',
        //managerCount: 0,
        //managers: {},
        //wmanagers: [],
        //topCmps: {},
        //managerIdPrev: 'eiapui',
        isDefined: function (v) {
            return typeof v !== 'undefined';
        },
        getBody: function () {
            return $("body");
        },
        onReady: function () {
            if (arguments)
                $(window).load(arguments[0]);
        },
        //给o扩充defaults对象中的属性，给o扩充c对象中的属性
        apply: function (o, c, defaults) {
            if (defaults) {
                EIAP.apply(o, defaults);
            }
            if (o && c && typeof c == 'object') {
                for (var p in c) {
                    o[p] = c[p];
                }
            }
            return o;
        },
        //如果对象c的属性在o中为undefined，则给o扩充c的属性
        applyIf: function (o, c) {
            if (o) {
                //for-in 枚举对象的属性
                for (var p in c) {
                    if (!EIAP.isDefined(o[p])) {
                        o[p] = c[p];
                    }
                }
            }
            return o;
        },
        //如果对象o的属性在c中有定义，则给o扩充c的函数
        applyHave: function (o, c) {
            if (o && c && typeof c == 'object') {
                for (var p in o) {
                    if (c[p] != undefined && c[p] != null)
                        o[p] = c[p];
                }
            }
            return o;
        },
        resizeAll: function () {
            var cmps = EIAP.managers;
            for (var cmp in cmps) {
                if (cmps[cmp] || cmps[cmp].type == "Window" && cmps[cmp].ismax === true) {
                    cmps[cmp].onResize();
                }
            }
            //            var cmps = EIAP.topCmps;
            //            for (var cmp in cmps) {
            //                if (cmps[cmp]) {
            //                    this.resize(cmps[cmp]);
            //                }
            //            }
        },
        resize: function () {
            var cmp = arguments[0];
            cmp.onResize();
            if (cmp.items) {
                for (var i = 0; i < cmp.items.length; i++)
                    EIAP.resize(EIAP.getCmp(cmp.items[i]));
            }
        },
        toArray: function () {
            return $.browser.msie ?
                function (a, i, j, res) {
                    res = [];
                    for (var x = 0, len = a.length; x < len; x++) {
                        res.push(a[x]);
                    }
                    return res.slice(i || 0, j || res.length);
                } :
                function (a, i, j) {
                    return Array.prototype.slice.call(a, i || 0, j || a.length);
                };
        }(),

        //覆盖：给origclass的原型prototype扩充overrides对象中的属性
        override: function (origclass, overrides) {
            if (overrides) {
                var p = origclass.prototype;
                EIAP.apply(p, overrides);
                //如果overrides存在实例属性toString
                //这是为了适应IE早期版本，
                //IE早期版本的实现中存在一个BUG，即屏蔽不可枚举属性的实例属性不会出现在for-in循环中
                if (overrides.hasOwnProperty('toString')) {
                    p.toString = overrides.toString;
                }
            }
        },
        extend: function () {
            // inline overrides
            var io = function (o) {
                for (var m in o) {
                    this[m] = o[m];
                }
            };
            var oc = Object.prototype.constructor;

            return function (sb, sp, overrides) {
                if (typeof sp == 'object') {
                    overrides = sp;
                    sp = sb;
                    sb = overrides.constructor != oc
                        ? overrides.constructor
                        : function () {
                            sp.apply(this, arguments);
                        };
                }
                var F = function () {
                }, sbp, spp = sp.prototype;

                F.prototype = spp;
                sbp = sb.prototype = new F();
                sbp.constructor = sb;
                sb.superclass = spp;
                if (spp.constructor == oc) {
                    spp.constructor = sp;
                }
                sb.override = function (o) {
                    EIAP.override(sb, o);
                };
                sbp.superclass = sbp.supr = (function () {
                    return spp;
                });
                sbp.override = io;
                EIAP.override(sb, overrides);
                sb.extend = function (o) {
                    return EIAP.extend(sb, o);
                };
                return sb;
            };
        }(),


    };
    EIAP.ns = EIAP.namespace;
    EIAP.ns("EIAP.core", "EIAP.form");
    // 组件基类
    // 1,完成定义参数处理方法和参数属性初始化的工作
    // 2,完成定义事件处理方法和事件属性初始化的工作
    EIAP.Component = $.eiapui.core.Component = function (options) {
        // 事件容器
        this.events = this.events || {};
        // 配置参数
        this.options = options || {};
        // 子组件集合索引
        this.items = null;
        // 初始化id
        if (!options || (!options.id && !options.renderTo)) {
            this.id = $.eiapui.getId(this.__idPrev());
        } else {
            this.id = options.id ? options.id : options.renderTo;
        }
    };
    //将{}中的方法扩展到$.eiapui.core.Component.prototype中
    $.extend($.eiapui.core.Component.prototype, {
        __getType: function () {
            return '$.eiapui.core.Component';
        },
        __idPrev: function () {
            return 'eiapui';
        },
        // 设置属性
        // arg 属性名 value 属性值
        // arg 属性/值 value 是否只设置事件
        set: function (arg, value) {
            var g = this;
            if (!arg)
                return;
            if (typeof arg == 'object') {
                var tmp;
                if (g != arg) {
                    $.extend(g, arg);
                    tmp = arg;
                } else {
                    tmp = $.extend({}, arg);
                }
                if (value == undefined || value == true) {
                    for (var p in tmp) {
                        if (p.indexOf('on') == 0)
                            g.set(p, tmp[p]);
                    }
                }
                if (value == undefined || value == false) {
                    for (var p in tmp) {
                        if (p.indexOf('on') != 0)
                            g.set(p, tmp[p]);
                    }
                }
                return;
            }
            var name = arg;
            // 事件参数
            if (name.indexOf('on') == 0) {
                if (typeof value == 'function')
                    g.bind(name.substr(2), value);
                return;
            }
            g.trigger('propertychange', arg, value);
            g[name] = value;
            var pn = '_set' + name.substr(0, 1).toUpperCase()
							+ name.substr(1);
            if (g[pn]) {
                g[pn].call(g, value);
            }
            g.trigger('propertychanged', arg, value);
        },

        // 获取属性
        get: function (name) {
            var pn = '_get' + name.substr(0, 1).toUpperCase()
							+ name.substr(1);
            if (this[pn]) {
                return this[pn].call(this, name);
            }
            return this.options[name];
        },

        hasBind: function (arg) {
            var name = arg.toLowerCase();
            var event = this.events[name];
            if (event && event.length)
                return true;
            return false;
        },

        // 触发事件
        // data (可选) Array(可选)传递给事件处理函数的附加参数
        trigger: function (arg, data) {
            var name = arg.toLowerCase();
            var event = this.events[name];
            if (!event)
                return;
            data = data || [];
            if ((data instanceof Array) == false) {
                data = [data];
            }
            for (var i = 0; i < event.length; i++) {
                var ev = event[i];
                if (ev.handler.apply(ev.context, data) == false)
                    return false;
            }
        },
        fireEvent: function () {
            var evt = arguments[0];
            var a = Array.prototype.slice.call(arguments, 1);
            if (this.events[evt]) {
                for (var i = 0; i < this.events[evt].length; i++) {
                    if (this.events[evt][i].apply(this, [a[0]]) == false) {
                        return false;
                    }
                }
            }
        },
        on: function () {
            var evts = arguments[0];
            var evtFn = arguments[1];
            if (typeof evts == 'object') {
                for (var p in evts) {
                    if (this.events[p]) {
                        this.events[p].push(evts[p]);
                    } else {
                        this.events[p] = [evts[p]];
                    }
                }
            } else {
                if (this.events[evts]) {
                    this.events[evts].push(evtFn);
                } else {
                    this.events[evts] = [evtFn];
                }
            }
        },
        un: function () {
            var evts = arguments[0];
            var evtFn = arguments[1];
            var i = 0, fn;
            if (typeof events == 'object') {
                for (var p in evts) {
                    if (this.events[p]) {
                        for (i = 0; i < this.events[p].length; i++) {
                            fn = this.events[p][i];
                            if (fn == evts[p]) {
                                this.events[p].splice(i, 1);
                                return;
                            }
                        }
                    }
                }
            } else {
                if (this.events[evts]) {
                    for (i = 0; i < this.events[evts].length; i++) {
                        fn = this.events[evts][i];
                        if (fn == evtFn) {
                            this.events[evts].splice(i, 1);
                            return;
                        }
                    }
                }
            }
        },
        // 绑定事件
        bind: function (arg, handler, context) {
            if (typeof arg == 'object') {
                for (var p in arg) {
                    this.bind(p, arg[p]);
                }
                return;
            }
            if (typeof handler != 'function')
                return false;
            var name = arg.toLowerCase();
            var event = this.events[name] || [];
            context = context || this;
            event.push({
                handler: handler,
                context: context
            });
            this.events[name] = event;
        },

        // 取消绑定
        unbind: function (arg, handler) {
            if (!arg) {
                this.events = {};
                return;
            }
            var name = arg.toLowerCase();
            var event = this.events[name];
            if (!event || !event.length)
                return;
            if (!handler) {
                delete this.events[name];
            } else {
                for (var i = 0, l = event.length; i < l; i++) {
                    if (event[i].handler == handler) {
                        event.splice(i, 1);
                        break;
                    }
                }
            }
        },
        destroy: function () {
            $.eiapui.remove(this);
        }
    });
    // 界面组件基类,
    // 1,完成界面初始化:设置组件id并存入组件管理器池,初始化参数
    // 2,渲染的工作,细节交给子类实现
    // @parm [options] 组件的参数
    EIAP.UIComponent = $.eiapui.core.UIComponent = function () {
        EIAP.apply(this, arguments[0]);
        EIAP.UIComponent.superclass.constructor.call(this, arguments[0]);
        this.initComponent();
        this.run();
        if (this.hidden && this.dom) {
            this.dom.hide();
        }
        return this;
    };
    EIAP.extend(EIAP.UIComponent, EIAP.Component, {
        doInit: function () {
            var extMethods = this._extMethods();
            if (extMethods)
                $.extend(this, extMethods);
            this.element = arguments[0];
            this._init();
            this._preRender();
            this.trigger('render');
            this._render();
            this.addItems();
            this.trigger('rendered');
            this._rendered();
        },
        show: function () {
            this.dom && this.dom.show();
        },
        hide: function () {
            this.dom && this.dom.hide();
        },
        run: function () {
            var curthis = this, args = [this.options];
            var dom = curthis.options.renderTo ? $("#" + curthis.options.renderTo) : window;
            var widget = curthis.__getType();
            if (!widget)
                return;
            var ext = $.extend({
                idAttrName: 'eiapuiid',
                isStatic: false,
                hasElement: true, // 是否拥有element主体(比如drag、resizable等辅助性插件就不拥有)
                propertyToElemnt: null
                // 链接到element的属性名
            }, arguments[0] || {});
            if (dom == null || dom == window || ext.isStatic) {
                if (!$.eiapui.widgets[widget]) {
                    $.eiapui.widgets[widget] = {
                        fn: $['eiap' + widget],
                        isStatic: true
                    };
                }
                return curthis.doInit();
            }
            if (!$.eiapui.widgets[widget]) {
                $.eiapui.widgets[widget] = {
                    fn: $.fn['eiap' + widget],
                    isStatic: false
                };
            }
            if (/Manager$/.test(widget))
                return $.eiapui.get(dom, ext.idAttrName);
            dom.each(function () {
                if (this[ext.idAttrName]
										|| $(widget).attr(ext.idAttrName)) {
                    var manager = $.eiapui
											.get(this[ext.idAttrName]
													|| $(this)
															.attr(ext.idAttrName));
                    if (manager && args.length > 0)
                        manager.set(args[0]);
                    // 已经执行过
                    return;
                }
                if (args.length >= 1
										&& typeof args[0] == 'string')
                    return;
                // 只要第一个参数不是string类型,都执行组件的实例化工作
                var options = args.length > 0 ? args[0] : null;
                var p = $.extend({}, options || {});
                if (ext.propertyToElemnt)
                    p[ext.propertyToElemnt] = this;
                if (ext.hasElement) {
                    curthis.doInit(this);
                } else {
                    curthis.doInit();
                }
            });
            if (curthis.length == 0)
                return null;
            if (args.length == 0)
                return $.eiapui.get(dom, ext.idAttrName);
            if (typeof args[0] == 'object')
                return $.eiapui.get(dom, ext.idAttrName);
            if (typeof args[0] == 'string') {
                var manager = $.eiapui.get(curthis, ext.idAttrName);
                if (manager == null)
                    return;
                if (args[0] == "option") {
                    if (args.length == 2)
                        return manager.get(args[1]); // manager get
                    else if (args.length >= 3)
                        return manager.set(args[1], args[2]); // manager
                    // set
                } else {
                    var method = args[0];
                    if (!manager[method])
                        return; // 不存在这个方法
                    var parms = Array.apply(null, args);
                    parms.shift();
                    return manager[method].apply(manager, parms); // manager
                    // method
                }
            }
            return null;
        },
        initComponent: function () {
            var items = this.options.items;
            this.items = [];
            if (items) {
                for (var i = 0; i < items.length; i++) {
                    items[i].parentCmp = this.id;
                    items[i].isDefined = true;
                }
            }
        },
        addItems: function () {
            var items = this.options.items;
            if (items) {
                for (var i = 0; i < items.length; i++) {
                    var item = items[i];
                    var xtype = item.xtype ? item.xtype : this.defaultType;
                    EIAP.applyIf(item, this.defaultStyle);
                    if (!xtype) {
                        throw new Error(EIAP.error.noXtype);
                    }
                    var cmp = eval("EIAP." + xtype);
                    if (!cmp) {
                        throw new Error(String.format(EIAP.error.noCmp, xtype));
                    }
                    cmp = cmp.call(cmp, item);
                    this.items.push(cmp.id);
                }
            }
        },

        __getType: function () {
            return '$.eiapui.core.UIComponent';
        },
        // 扩展方法
        _extMethods: function () {

        },
        _init: function () {
            this.type = this.__getType();

            // 存入管理器池
            $.eiapui.add(this);

            if (!this.element)
                return;

            // 读取attr方法,并加载到参数,比如['url']
            var attributes = this.attr();
            if (attributes && attributes instanceof Array) {
                for (var i = 0; i < attributes.length; i++) {
                    var name = attributes[i];
                    this[name] = $(this.element).attr(name);
                }
            }
            // 读取eiapui这个属性，并加载到参数，比如 eiapui = "width:120,heigth:100"
            if ($(this.element).attr("eiapui")) {
                try {
                    var attroptions = $(this.element).attr("eiapui");
                    if (attroptions.indexOf('{') != 0)
                        attroptions = "{" + attroptions + "}";
                    eval("attroptions = " + attroptions + ";");
                    if (attroptions)
                        $.extend(this, attroptions);
                } catch (e) {
                }
            }
        },
        // 预渲染,可以用于继承扩展
        _preRender: function () {

        },
        _render: function () {

        },
        _rendered: function () {
            if (this.element) {
                $(this.element).attr("eiapuiid", this.id);
            } else {
                this.dom.attr("eiapuiid", this.id);
            }
        },
        // 返回要转换成eiapui参数的属性,比如['url']
        attr: function () {
            return [];
        },
        addItem: function (cmp) {
            cmp.parentCmp = this.id;
            cmp.isDefined = true;
            this.items.push(cmp.id);
            var dom = this.getDom();
            dom.append(cmp.dom);
        },
        insertItem: function (cmp, index) {
            if (typeof index == "number" && index >= 0 && index < this.items.length) {
                cmp.parentCmp = this.id;
                cmp.isDefined = true;
                var num = 0;
                for (var i = 0; i < this.items.length; i++) {
                    var item = EIAP.getCmp(this.items[i]);
                    if (item.isDefined) {
                        if (num == index) {
                            this.items.slice(cmp, 0, i + 1);
                            var dom = item.getDom();
                            dom.after(cmp.dom);
                            return;
                        }
                        num++;
                    }
                }
            }
        },
        getDom: function () {
            return this.dom;
        },
        onResize: function () {
        },
        addToTop: function () {
            var parent = this.dom ? this.dom.parent() : null;
            if (this.type != "Window" && parent && parent[0] && parent[0].tagName.toLowerCase() == "body") {
                if (!EIAP.topCmps[this.id])
                    EIAP.topCmps[this.id] = this;
            }
        }
    });
    EIAP.draggable = {
        dragging: false
    };
    EIAP.resizable = {
        reszing: false
    };

    EIAP.CustomUI = function () {
        EIAP.apply(this, arguments[0]);
        this.initComponent();
        return this;
    };
    EIAP.CustomUI.prototype = {
        initComponent: function () {

        }
    };

})(jQuery);



EIAP.form.Field = EIAP.extend(EIAP.UIComponent, {
    inputType: 'text',
    readonly: false,
    name: null,
    field: null,
    submitValue: null,
    isFormField: true,
    title: null,
    value: null,
    showTitle: true,
    labelFirst: true,
    invalidText: null,
    domCss: "ux-field",
    readonlyCss: "ux-field-readonly",
    disabledCss: "ux-field-disable",
    labelCss: "ux-field-label",
    elementCss: "ux-field-element",
    requiredCss: "ux-field-must",
    initComponent: function () {
        EIAP.form.Field.superclass.initComponent.call(this);
        if (!this.labelFirst) {
            this.labelCss = "ux-field-label-right";
        }
    },

    __getType: function () {
        return 'Field';
    },

    __idPrev: function () {
        return 'Field';
    },
    _extMethods: function () {
        return EIAP.form.Field;
    },
    _render: function () {
        var g = this;
        g.dom = g.renderTo ? $(g.element) : $("<div></div>");
        g.dom.addClass(g.domCss);
        g.initLabel();
        g.initField();
        g.addCss();
        g.initAttribute();
        g.addEvents();
        if (g.value != null && g.value != "" && g.value != undefined) {
            if (!(typeof g.value == "object")) {
                g.setValue(g.value);
            } else
                g.loadData(g.value);
        } else {
            g.initSubmitValue();
        }
    },
    show: function () {
        EIAP.form.Field.superclass.show.call(this);
        this.hidden = false;
    },
    hide: function () {
        EIAP.form.Field.superclass.hide.call(this);
        this.hidden = true;
    },
    getLabel: function () {
        var g = this, v = "";
        if (g.title) {
            v = g.title;     //.replace(/[^\u4e00-\u9fa5]/gi, "")   (表单元素title去除英文筛选)
        }
        return v;
    },
    initLabel: function () {
        var g = this;
        if (g.title) {
            var titleText = !g.labelFirst ? g.title : g.title + "：";
            var label = $("<label>" + titleText + "</label>");
            if (g.showTitle) label.attr("title", g.getLabel());
            label.addClass(g.labelCss);
            g.dom.label = label;
            g.dom.append(label);
            g.setLabelWidth(g.labelWidth ? g.labelWidth : 70);
        }
    },
    setTitle: function () {
        var g = this, ags = arguments;
        if (ags.length > 0) {
            g.title = ags[0];
            g.dom.label.html(!g.labelFirst ? ags[0] : ags[0] + "：");
        }
    },
    setLabelWidth: function () {
        var width = parseInt(arguments[0]);
        if (this.dom.label && width) {
            this.dom.label.width(width);
        }
    },
    initField: function () {
        var g = this;
        var input = $("<input type='" + g.inputType + "'>");
        input.addClass(g.fieldCss);
        g.dom.field = input;
        g.dom.fieldwrap = $("<div></div>").addClass(g.elementCss);

        g.dom.fieldwrap.append(g.dom.field);
        if (g.labelFirst) {
            g.dom.append(g.dom.fieldwrap);
        } else {
            g.dom.fieldwrap.insertBefore(g.dom.label);
        }
        g.dom.append('<div class="ux-clear"></div>');
    },

    addCss: function () {
        var g = this;
        if (g.readonly) {
            g.dom.field.attr("readonly", g.readonly);
            g.dom.field.addClass(g.readonlyCss);
        }
        //        if (g.disabled) {
        //            g.dom.attr("disabled", g.disabled);
        //            g.dom.addClass(g.disabledCss);
        //        }
    },

    initAttribute: function () {
        var g = this;
        if (g.readonly) {
            g.dom.field.addClass(g.readonlyCss);
        }
        if (g.disabled) {
            g.dom.field.attr("disabled", g.disabled);
        }
        if (g.name) {
            g.dom.field.attr("name", g.name);
        }
        if (g.submitValue) {
            g.dom.field.data('submitValue', g.submitValue);
        }
        g.dom.field.attr("name", g.name);
    },
    addEvents: function () {
        var g = this;
        g.dom.field.bind("click", function () {
            if (!g.dom.field.hasClass(g.focusCss)) {
                g.dom.field.addClass(g.focusCss);
            }
        });

        if (this.handler) {
            g.dom.field.bind("click", this.handler);
        }
        var listener = this.listener;
        if (listener) {
            for (var key in listener)
                g.dom.field.bind(key, listener[key]);
        }
    },

    sysValidater: function () {

    },
    keyup: function () {
        this.sysValidater();
    },

    getValue: function () {
        return this.dom.field.val();
    },
    initSubmitValue: function () {
        var g = this, value = {};
        if (g.field) {
            for (var i = 0; i < g.field.length; i++) {
                var name = g.field[i];
                value[name] = "";
            }
        }
        g.submitValue = value;
    },
    getSubmitValue: function () {
        var data = {};
        if (this.name)
            data[this.name] = this.getValue();
        EIAP.applyIf(data, this.submitValue);
        return data;
    },
    setSubmitValue: function () {
        var g = this, value = {};
        var data = arguments[0];
        if (data && g.field) {
            for (var i = 0; i < g.field.length; i++) {
                var name = g.field[i];
                value[name] = (data[name] != undefined && data[name] != null ? data[name] : "");
            }
        }
        g.submitValue = value;
    },
    loadData: function () {
        var data = arguments[0];
        this.setSubmitValue(data);
        if (data) {
            this.setValue(data[this.name]);
        }
    },
    setReadOnly: function () {
        var args = arguments;
        if (args.length != 0) {
            var arg = args[0] == "true" || args[0] == true ? true : false;
            this.dom.field.attr("readonly", arg);
            this.readonly = arg;
            if (arg) {
                if (!this.dom.field.hasClass(this.readonlyCss)) {
                    this.dom.field.addClass(this.readonlyCss);
                }
            } else {
                this.dom.field.removeClass(this.readonlyCss);
            }
        }
    },
    setWidth: function () {
        this.dom.field.width(arguments[0]);
    },
    setValue: function () {
        this.dom.field.val(arguments[0]);
    },
    reset: function () {
        this.dom.field.val("");
        this.initSubmitValue();
    },
    showTip: function () {
        EIAP.remove(this.tooltip);
        this.tooltip = EIAP.ToolTip({ target: this.dom.field, isInvalid: true, tipText: this.invalidText });
    }
});

EIAP.TextField = function () {
    return new EIAP.form.TextField(arguments[0]);
};

EIAP.form.TextField = EIAP.extend(EIAP.form.Field, {
    inputType: 'text',
    width: 160,
    height: 18,
    minlength: 0,
    maxlength: Number.MAX_VALUE,
    allowChar: null,
    allowBlank: true,
    validater: null,
    validateText: null,
    displayText: null,
    displayCss: "ux-field-display",
    fieldCss: "ux-field-text",
    focusCss: "ux-field-focus",
    fieldStyle: null,
    invalidCss: "ux-field-invalid",
    initComponent: function () {
        EIAP.form.TextField.superclass.initComponent.call(this);
    },
    __getType: function () {
        return 'TextField';
    },

    __idPrev: function () {
        return 'TextField';
    },

    _extMethods: function () {
        return EIAP.form.TextField;
    },
    _render: function () {
        EIAP.form.TextField.superclass._render.call(this);
        this.addFiledStyle();
    },
    initField: function () {
        EIAP.form.TextField.superclass.initField.call(this);
        this.setWidth(this.width);
        this.setHeight(this.height);
        this.initDisplayText();
    },
    initLabel: function () {
        var g = this;
        if (g.title) {
            var titleText = !g.labelFirst ? g.title : g.title + "：";
            titleText = !g.allowBlank ? (titleText + "<span class='" + g.requiredCss + "'>*</span>") : titleText;
            var label = $("<label>" + titleText + "</label>");
            if (g.showTitle) label.attr("title", g.getLabel());
            label.addClass(g.labelCss);
            g.dom.label = label;
            g.dom.append(label);
            g.setLabelWidth(g.labelWidth ? g.labelWidth : 70);
        }
    },
    setAllowBlank: function (v) {
        var g = this;
        if (v === false) {
            if (g.dom.label.find("span." + g.requiredCss).length > 0) return;
            g.dom.label.append("<span class='" + g.requiredCss + "'>*</span>");
            g.allowBlank = false;
        } else {
            g.dom.label.find("span." + g.requiredCss).remove();
            g.allowBlank = true;
        }
    },
    setWidth: function () {
        var width = parseInt(arguments[0]);
        if (width) {
            if (typeof arguments[0] == "string" && arguments[0].indexOf("%") > -1) {
                width /= 100;
                width *= this.dom.parent().width();
                if (this.dom.label) {
                    if (!this.labelWidth) {
                        if (width * 7 / 23 < 40) {
                            this.setLabelWidth(40);
                            width -= 40;
                        }
                        if (width * 7 / 23 > 70) {
                            this.setLabelWidth(70);
                            width -= 70;
                        } else {
                            this.setLabelWidth(width * 7 / 23);
                            width -= (width * 7 / 23);
                        }
                    } else {
                        this.setLabelWidth(this.labelWidth);
                        width -= this.labelWidth;
                    }
                }
                width -= 10;
            }
            this.dom.field.width(width);
            this.width = arguments[0];
        }
    },
    setHeight: function () {
        var height = parseInt(arguments[0]);
        if (height) {
            this.dom.field.height(height);
            this.dom.height(height + parseFloat(this.dom.css("marginBottom")));

        }
    },
    initDisplayText: function () {
        var g = this;
        if (g.displayText) {
            g.dom.field.val(g.displayText);
            g.dom.field.addClass(g.displayCss);
        }
    },
    addEvents: function () {
        var g = this;
        var field = g.dom.field;
        field.bind("keypress", function (e) {
            var code = e.keyCode || e.charCode;
            if (code == 37 || code == 39 || code == 46 || code == 8 || (e.ctrlKey && code > 0)) {
                if (e.ctrlKey && (code == 118 || code == 86)) {
                    g.sysValidater();
                }
                return true;
            } else {
                var chr = String.fromCharCode(code);
                if (g.allowChar && g.allowChar.indexOf(chr) == -1) {
                    return false;
                }
            }
        });
        field.hover(function () {
            if (!field.hasClass(g.invalidCss)) {
                field.addClass(g.focusCss);
            }
        }, function () {
            field.removeClass(g.focusCss);
        });
        field.bind("blur", function () {
            field.removeClass(g.focusCss);
        });
        if (g.displayText) {
            g.doDisplayEvents();
        } else {
            g.doBlankEvents();
        }
        EIAP.form.TextField.superclass.addEvents.call(this);
    },
    doDisplayEvents: function () {
        var g = this;
        g.dom.field.bind("blur", function () {
            var value = g.dom.field.val();
            if (!value) {
                g.dom.field.addClass(g.displayCss);
                g.dom.field.val(g.displayText);
                g.removeFiledStyle();
            }
            g.dom.field.removeClass(g.focusCss);
            g.sysValidater();
            g.dom.field.removeClass(g.focusCss);
        });
        g.dom.field.bind("keyup", function () {
            g.sysValidater();
        });
        g.dom.field.bind("click", function () {
            if (!g.readonly) {
                var value = g.dom.field.val();
                if (g.displayText == value) {
                    g.dom.field.val("");
                }
                g.dom.field.removeClass(g.displayCss);
                g.sysValidater();
            }
        });
    },
    doBlankEvents: function () {
        var g = this;
        g.dom.field.bind("keyup", function () {
            g.sysValidater();
        });
        g.dom.field.bind("blur", function () {
            g.sysValidater();
            g.dom.field.removeClass(g.focusCss);
        });
    },

    sysValidater: function () {
        var g = this;
        if (g.hidden)
            return true;
        var value = g.getValue();
        if (g.validater) {
            if (g.validater(value) == false) {
                g.invalidText = g.validateText ? g.validateText : "";
                g.afterValid(false);
                return false;
            }
        }
        if (g.allowChar && value.length > 0 && g.displayText != value) {
            for (var i = 0; i < value.length; i++) {
                var c = value.charAt(i);
                if (g.allowChar.indexOf(c) == -1) {
                    g.invalidText = String.format(g.invalidCharText, value, c);
                    g.afterValid(false);
                    return false;
                }
            }
        }
        if (!g.checkLength()) {
            g.afterValid(false);
            return false;
        }
        if (!g.allowBlank && (value == null || value === "" || g.displayText == value || value.length == 0)) {
            g.invalidText = g.blankText;
            g.afterValid(false);
            return false;
        }
        g.afterValid(true);
        return true;
    },
    afterValid: function () {
        var g = this, flag = arguments[0];
        if (!flag) {
            if (!g.dom.field.hasClass(g.invalidCss)) {
                g.dom.field.addClass(g.invalidCss);
                g.dom.field.removeClass(g.focusCss);
            }
            g.showTip();
        } else {
            g.dom.field.removeClass(g.invalidCss);
            g.dom.field.removeClass(g.focusCss);
            if (g.tooltip) {
                g.tooltip.close();
                g.tooltip = undefined;
            }
        }
    },
    checkLength: function () {
        var g = this;
        var value = g.dom.field.val();
        if (value.length < g.minlength) {
            g.invalidText = String.format(g.minLengthText, g.minlength);
            return false;
        }
        if (value.length > g.maxlength) {
            g.invalidText = String.format(g.maxLengthText, g.maxlength);
            return false;
        }
        return true;
    },
    addFiledStyle: function () {
        var g = this;
        if (g.fieldStyle) {
            var fcss = g.dom.field.attr("style");
            if (typeof g.fieldStyle == "string") {
                var curcss = EIAP.util.DomStyle.cssToObj(fcss);
                var tcss = EIAP.util.DomStyle.cssToObj(g.fieldStyle);
                var style = EIAP.apply(curcss, tcss);
                var cssStr = EIAP.util.DomStyle.objToCss(style);
                g.dom.field.attr("style", cssStr);
            }
        }
    },
    removeFiledStyle: function () {
        var g = this;
        if (g.fieldStyle) {
            var fcss = g.dom.field.attr("style");
            if (typeof g.fieldStyle == "string") {
                var curcss = EIAP.util.DomStyle.cssToObj(fcss);
                var tcss = EIAP.util.DomStyle.cssToObj(g.fieldStyle);
                var cssObj = EIAP.util.DomStyle.removeStyle(curcss, tcss);
                var cssStr = EIAP.util.DomStyle.objToCss(cssObj);
                g.dom.field.attr("style", cssStr);
            }
        }
    },
    getValue: function () {
        var g = this, v = g.dom.field.val();
        if (g.displayText === v) {
            return "";
        }
        return g.dom.field.val();
    },
    setValue: function () {
        var g = this;
        EIAP.form.TextField.superclass.setValue.call(this, arguments[0]);
        if (arguments.length != 0 && arguments[0]) {
            g.dom.field.removeClass(g.displayCss);
        } else {
            g.initDisplayText();
        }
    },
    onResize: function () {
        var g = this;
        if (typeof g.width == "string" && g.width.indexOf("%") > -1) {
            g.setWidth(g.width);
        }
    },
    reset: function () {
        var g = this;
        if (!g.displayText || g.displayText == "") {
            g.dom.field.val("");
        } else {
            g.initDisplayText();
        }
        this.initSubmitValue();
    }
});